<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
String headPath = request.getContextPath();
String headerBasePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+"/";
%>
<script>
	function logout(){
		// 创建对象
		var xmlHttp;
		if (window.XMLHttpRequest) {
			xmlHttp = new XMLHttpRequest();
		} else {
			xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		}

		var url = getBasePath() + "user/back_logout";
		// 发送请求
		xmlHttp.open("get",url,true);
		xmlHttp.send();

		// 返回数据
		xmlHttp.onreadystatechange = function() {
			if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
				var res = JSON.parse(xmlHttp.responseText);
				console.log(typeof res);
				console.log("返回内容是: " + res);
				if (res.isSuccess) {
					localStorage.setItem('userId','');
		        	localStorage.setItem('username', '');
		        	localStorage.setItem('type', '');
		        	alert("退出成功!");
		      		location.href = getBasePath();
				}
			}
		}
	}

	function getBasePath() {
		var obj = window.location;
		var contextPath = obj.pathname.split("/")[1];
		var basePath = obj.protocol+"//"+ obj.host +"/";
		return basePath;
	}

</script>
<div>
	<div>
		<a href="<%=headerBasePath%>"><img src="<%=headerBasePath%>resources/imgs/logo.png"></a>
	</div>
	<div id="in-header">
		<ul>
			<c:if test="${loginedUsername == null}">
				<li><a href="<%=headerBasePath%>login">登录</a></li>
			</c:if>
			<c:if test="${loginedUsername == null}">
				<li><a href="<%=headerBasePath%>register">注册</a></li>
			</c:if>
			<c:if test="${loginedUsername != null}">
				<li><a href="<%=headerBasePath%>user/center">${loginedUsername}的个人中心</a></li>
			</c:if>
			<c:if test="${loginedUsername != null && loginedUserType == 1}">
				<li><a href="<%=headerBasePath%>admin/user">用户列表</a></li>
			</c:if>
			<c:if test="${loginedUsername != null && loginedUserType == 1}">
				<li><a href="<%=headerBasePath%>admin/question">试题中心</a></li>
			</c:if>
			<c:if test="${loginedUsername != null}">
				<li><a href="#" title="" onclick="logout()">退出</a></li>
			</c:if>
		</ul>
	</div>
</div>